import { Reducer } from 'redux';
import { Effect } from 'dva';

import {
  commodityAdd,
  commodityList,
  commodityDelete,
  commodityEdit,
  commodityDown,
  commodityUpload,
  commodityType,
  commodityUtil,
} from '@/services/user';
import { notification } from 'antd';
export interface StateType {
  isRefresh?: boolean;
  typeData?: any;
  total?: number;
  list?: any;
  view?: any;
  edit?: any;
  utillist?: any;
}

export interface LoginModelType {
  namespace: string;
  state: StateType;
  effects: {
    add: Effect;
    view: Effect;
    put: Effect;
    edit: Effect;
    delete: Effect;
    list: Effect;
    down: Effect;
    upload: Effect;
    type: Effect;
    utils: Effect;
  };
  reducers: {
    redView: Reducer<StateType>;
    redEdit: Reducer<StateType>;
    redList: Reducer<StateType>;
    reFresh: Reducer<StateType>;
    reType: Reducer<StateType>;
    reUtils: Reducer<StateType>;
  };
}

const Model: LoginModelType = {
  namespace: 'commodity',

  state: {
    isRefresh: false,
    typeData: [],
    utillist: [],
    total: 0,
    list: [],
    view: {},
    edit: {},
  },

  effects: {
    *add({ payload }, { call, put }) {
      const res = yield call(commodityAdd, payload);
      if (res.status == 200) {
        notification.success({
          message: '增加成功',
          description: '',
        });
        yield put({
          type: 'reFresh',
          payload: true,
        });
      }
    },
    *put({ payload }, { call, put }) {
      const res = yield call(commodityEdit, payload);
      if (res.status == 200) {
        notification.success({
          message: '商品修改成功',
          description: '',
        });
        yield put({
          type: 'reFresh',
          payload: true,
        });
      }
    },
    *view({ payload }, { put }) {
      yield put({
        type: 'redView',
        payload,
      });
    },
    *edit({ payload }, { call, put }) {
      yield put({
        type: 'edit',
        payload,
      });
    },
    *delete({ payload }, { call, put }) {
      const res = yield call(commodityDelete, payload);
      if (res.status == 200) {
        notification.success({
          message: '删除成功',
          description: '',
        });
        yield put({
          type: 'reFresh',
          payload: true,
        });
      }
    },
    *list({ payload }, { call, put }) {
      const res = yield call(commodityList, payload);
      if (res.status == 200) {
        yield put({
          type: 'redList',
          payload: { data: res.data.content, total: res.data.totalElements },
        });
      }
    },
    *down({ payload }, { call, put }) {
      const response = yield call(commodityDown);
      const blob = new Blob([response], {
        type:
          'application/application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8',
      }); //application/vnd.openxmlformats-officedocument.spreadsheetml.sheet这里表示xlsx类型
      const downloadElement = document.createElement('a');
      const href = window.URL.createObjectURL(blob); //创建下载的链接
      downloadElement.href = href;
      downloadElement.download = '酒类模版.xlsx'; //下载后文件名
      document.body.appendChild(downloadElement);
      downloadElement.click(); //点击下载
      document.body.removeChild(downloadElement); //下载完成移除元素
      window.URL.revokeObjectURL(href); //释放掉blob对象
    },
    *upload({ payload }, { call, put }) {
      const res = yield call(commodityUpload, payload);
      if (res.status == 200) {
        notification.success({
          message: '上传成功',
          description: '',
        });
        yield put({
          type: 'reFresh',
          payload: true,
        });
      }
    },
    *type({ payload }, { call, put }) {
      const res = yield call(commodityType);
      // if (res.status == 200) {
      yield put({
        type: 'reType',
        payload: res,
      });
      // }
    },
    *utils({ payload }, { call, put }) {
      const response = yield call(commodityUtil, payload);
      if (response.status == 200) {
        yield put({
          type: 'reUtils',
          payload: response.data,
        });
      }
    },
  },

  reducers: {
    redView(state, { payload }) {
      return {
        ...state,
        view: payload,
      };
    },
    redEdit(state, { payload }) {
      return {
        ...state,
        edit: payload,
      };
    },
    redList(state, { payload }) {
      return {
        ...state,
        list: payload.data,
        total: payload.total,
        isRefresh: false,
      };
    },
    reType(state, { payload }) {
      return {
        ...state,
        typeData: payload,
      };
    },
    reFresh(state, { payload }) {
      return {
        ...state,
        isRefresh: payload,
      };
    },
    reUtils(state, { payload }) {
      return {
        ...state,
        utillist: payload,
      };
    },
  },
};

export default Model;
